// pages/category/category.js
import {Category} from "category-model.js";

var Categorys  = new Category();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._loadData();
  },

  _loadData:function(){
    var that = this;
    Categorys.getCategoryList(function(categoryData){
      console.log(categoryData);
      that.setData({
        "categorys":categoryData
      });
      that.setData({
        "categoryID":categoryData[0].id
      });

      Categorys.getProductByCateId(categoryData[0].id,function(products){
          console.log(products);
          var dataObj = {
            products:products,
            topImgUrl:categoryData[0].img.url,
            title:categoryData[0].name
          }

          that.setData({
            "categoryInfo":dataObj
          });
          

      });
    });   
  },

  // 获取相应分类详情
  onProductByCategory:function(event){
    var that = this;
    var id = Categorys.getDataSet(event,'id');
    var name = Categorys.getDataSet(event,'name');
    var img = Categorys.getDataSet(event,'img');
    Categorys.getProductByCateId(id,function(products){
      var dataObj = {
        products:products,
        topImgUrl:img,
        title:name
      }
      that.setData({
        "categoryInfo":dataObj
      });

      that.setData({
        "categoryID":id
      });
    });
  },

 // 跳转商品详情页面
 onProductsItemTap:function(event){
  var id = Categorys.getDataSet(event,'id');
    wx.navigateTo({
      url: '../product/product?id='+id,
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})